﻿using Microsoft.Samples.Debugging.Native;

namespace TPOJ.Core.Utils
{
    /// <summary>
    /// Utilities and helper methods.
    /// </summary>
    public static class Utils
    {
        /// <summary>
        /// Format the runtime error message from runtime exception.
        /// </summary>
        /// <param name="exceptionCode">Exception code</param>
        /// <returns>Runtime error message</returns>
        public static string GetExceptionMessage(this ExceptionCode exceptionCode)
        {
            uint code = (uint)exceptionCode;
            switch (code)
            {
                case (uint)ExceptionCode.EXCEPTION_ACCESS_VIOLATION:
                    return "Attempted to read or write protected memory. This is often an in dication that other memory is corrupt.";
                case (uint)ExceptionCode.EXCEPTION_INT_DIVIDE_BY_ZERO:
                    return "Attempted to divide by zero.";
                case (uint)ExceptionCode.EXCEPTION_STACK_OVERFLOW:
                    return "Operation caused a stack overflow.";
                case 3765269347:
                    return "Arithmetic operation resulted in an overflow.";
            }
            return "Code:" + exceptionCode.ToString();
        }

        /// <summary>
        /// Get coresponding test output data file name.
        /// By replacing xx.in to xx.out.
        /// </summary>
        /// <param name="inputFileName">Input data file name</param>
        /// <returns>Output data file name</returns>
        public static string GetOutputFileName(this string inputFileName)
        {
            return inputFileName.Remove(inputFileName.Length - 2) + "out";
        }

        /// <summary>
        /// Format the warning message generated by the compiler.
        /// </summary>
        /// <param name="message">The message generated by the compiler</param>
        /// <returns>Warning message for print</returns>
        public static string FormatCompileWarning(this string message)
        {
            int index;
            if (string.IsNullOrEmpty(message))
                return message;
            index = message.IndexOf("warning");
            if(index>=0)
                return "Compiler Warning: " + message.Trim().Substring(index);
            index = message.IndexOf("error");
            if (index >= 0)
                return "Compile Error:" + message.Trim().Substring(index);
            return message;
        }
    }
}
